package com.jservice.web.sysOffice;

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SysOffice;
import com.jservice.model.SysUser;
import com.jservice.web.base.JBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * SysOfficeQuery
 *
 * @author cwvian
 * @version 1.0
 * @date 2017/5/11
 */
@Service("sysOfficeQuery")
@Transactional(readOnly = true)
public class SysOfficeService extends JBaseService {

	public static final String TREE_PATH_SEPARATOR = ",";

	public static final SysOffice DAO = new SysOffice();

	public static SysOfficeService me() {
		return SpringContextUtil.getBean("sysOfficeQuery");
	}

	@Resource
	private Sequence idWorKer;

	public List<SysOffice> findTree(){
		return findChildren(null);
	}

	public List<SysOffice> findChildren(SysOffice office){
		List<SysOffice> offices = null;

		if(office != null){
			String sql = " select * from "+SysOffice.TABLE_NAME+" where tree_path like ?  order by orders asc ";
			offices = DAO.find(sql, "%" + TREE_PATH_SEPARATOR+ office.getId() + TREE_PATH_SEPARATOR + "%");

		}else{
			String sql = " select * from "+SysOffice.TABLE_NAME+" order by orders asc ";
			offices = DAO.find(sql);

		}
		return sort(offices,office);
	}

	@Transactional
	public boolean save(SysOffice office){
		office.setId(idWorKer.nextIdStr());
		office.setCreateDate(new Date());
		office.setModifyDate(office.getCreateDate());
		setValue(office);
		return office.save();
	}

	@Transactional
	public boolean update(SysOffice office){
		office.setModifyDate(new Date());
		setValue(office);

		return office.update();
	}

	@Transactional
	public boolean delete(String id){
		DAO.deleteById(id);
		Db.update("update "+ SysUser.TABLE_NAME+" set department = null where department = ? ",id);

		return true;
	}


	public List<String> getTreePaths(SysOffice office){
		List<String> treePaths = Lists.newArrayList();
		if(office == null){
			return treePaths;
		}

		String[] ids = StringUtils.split(office.getTreePath(), TREE_PATH_SEPARATOR);
		if (ids != null) {
			for (String id : ids) {
				treePaths.add(id);
			}
		}
		return treePaths;
	}

	private List<SysOffice > sort(List<SysOffice> offices, SysOffice parent){
		List<SysOffice> result = Lists.newArrayList();
		if(offices != null){
			for(SysOffice office : offices){
				if((StringUtils.isNotBlank(office.getParent()) && parent != null && parent.getId().equals(office.getParent())) ||
						(StringUtils.isBlank(office.getParent()) && parent == null) ){
					result.add(office);
					result.addAll(sort(offices,office));
				}
			}
		}

		return result;
	}

	private void setValue(SysOffice office){
		if(office == null){
			return;
		}
		if(StringUtils.isNotBlank(office.getParent())){
			SysOffice parent = DAO.findById(office.getParent());
			if(parent != null){
				office.setTreePath(parent.getTreePath() + parent.getId() + TREE_PATH_SEPARATOR);
			}else{
				office.setTreePath(TREE_PATH_SEPARATOR);
			}
		}else{
			office.setTreePath(TREE_PATH_SEPARATOR);
		}

		office.setGrade(getTreePaths(office).size());
	}



}
